﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;
using Zor_Bll;
using Zor_Modes;
using Zor_Tools;
using Zor_Web.Models;
using Zor_Web.Models.ReportForms;

namespace Zor_Web.Controllers
{
    [Authorization]
    public class ReportFormsController : Controller
    {
        //
        // GET: /ReportForms/

        #region - 综合治税统计报表控制器 -
        
        //定义街道办ID
        private static int StreetOfficeId;
        public ActionResult GIS_StatisticalTable()
        {
            return View();
        }

        /// <summary>
        /// 综合治税街道办统计报表
        /// create by Guoph at 2017.4.12
        /// </summary>       
        /// <returns></returns>
        public ActionResult LoadStreetOfficeInfo(string stateDate,string endDate)
        {   
           
            List<V_GIS_StatisticalTable> lst_StreetOfficeInfoGIS = GIS_StatisticalTable_Bll<V_GIS_StatisticalTable>.StreetOfficeInfoInfo(stateDate,endDate);
            return Content(lst_StreetOfficeInfoGIS.ToJson());
        }     

        /// <summary>
        /// 综合治税社区统计报表
        /// </summary>
        /// <param name="StreetOffice_Id"></param>
        /// <returns></returns>
        public ActionResult LoadCommunityModal(int StreetOffice_Id, string dateWhere)
        {
            StreetOfficeId = StreetOffice_Id;
            List<V_GIS_StatisticalTable> lst_CommunityGIS = GIS_StatisticalTable_Bll<V_GIS_StatisticalTable>.CommunityInfo(StreetOffice_Id, dateWhere);           
            return Content(lst_CommunityGIS.ToJson());
        }


        /// <summary>
        /// 导出街道办统计报表
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        public FileResult GIS_StatisticalTableMeg(FormCollection form)
        {
            string GIS_StatisticalTableMeg = form["GIS_StatisticalTableMeg"];
            string where = form["where"];
            string[] str_arr = GIS_StatisticalTableMeg.Split(',');
            NoSortHashTable nameList = new NoSortHashTable();

            for (int i = 0; i < str_arr.Length; i++)
            {
                if (str_arr[i] != "")
                {
                    string[] item = str_arr[i].Split('|');
                    nameList.Add(item[0], item[1]);
                }
            }
            #region 拼接table

            System.Text.StringBuilder data = new System.Text.StringBuilder();
            if (nameList != null)
            {
                data.Append("<table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\">");
                //写出列名
                data.Append("<tr style=\"font-weight: bold; white-space: nowrap;\">");

                foreach (string s in nameList.Keys)
                {
                    data.Append("<td>" + nameList[s] + "</td>");
                }               
                data.Append("</tr>");

                string stateDate = "";
                string endDate = "";
                if (!string.IsNullOrEmpty(where))
                {
                    string[] getTime = where.Split(',');
                     stateDate = getTime[0];
                     endDate = getTime[1];

                }
                

                List<V_GIS_StatisticalTable> list = GIS_StatisticalTable_Bll<V_GIS_StatisticalTable>.StreetOfficeInfoInfo(stateDate,endDate);
                //写出数据
                int StreetOfficeHousNumber = 0, StreetOfficeShopNumber = 0, ANumber = 0, PNumber =0;
                double Zzs = 0, Fcs = 0, Grsds = 0, Cjs = 0, Jyffj = 0, Dfjyfj = 0, Yhs = 0, Sljj = 0, xiaoji = 0;                            
                foreach (V_GIS_StatisticalTable model in list)
                {
                    data.Append("<tr>");
                    foreach (string sm in nameList.Keys)
                    {
                        switch (sm)
                        {
                            case "StreetOffice": data.Append("<td>" + model.StreetOffice + "</td>"); ; break;
                            case "StreetOfficeHousNumber": 
                                data.Append("<td>" + model.StreetOfficeHousNumber + "</td>");
                                StreetOfficeHousNumber += model.StreetOfficeHousNumber; 
                                break;
                            case "StreetOfficeShopNumber": 
                                data.Append("<td>" + model.StreetOfficeShopNumber + "</td>");
                                StreetOfficeShopNumber += model.StreetOfficeShopNumber;
                                break;
                            case "ANumber": 
                                data.Append("<td>" + model.ANumber + "</td>");
                                ANumber += model.ANumber; 
                                break;
                            case "PNumber": 
                                data.Append("<td>" + model.PNumber + "</td>");
                                PNumber += model.PNumber; 
                                break;
                            case "Zzs": 
                                 data.Append("<td>" + model.Zzs + "</td>");
                                 Zzs += model.Zzs; 
                                 break;
                            case "Fcs": 
                                data.Append("<td>" + model.Fcs + "</td>");
                                Fcs += model.Fcs; 
                                break;
                            case "Grsds": 
                                data.Append("<td>" + model.Grsds + "</td>");
                                Grsds += model.Grsds;
                                break;
                            case "Cjs": 
                                 data.Append("<td>" + model.Cjs + "</td>");
                                 Cjs += model.Cjs; 
                                 break;
                            case "Jyffj": 
                                data.Append("<td>" + model.Jyffj + "</td>");
                                Jyffj += model.Jyffj; 
                                break;
                            case "Dfjyfj": 
                                 data.Append("<td>" + model.Dfjyfj + "</td>");
                                 Dfjyfj += model.Dfjyfj;
                                 break;
                            case "Yhs": 
                                data.Append("<td>" + model.Yhs + "</td>");
                                Yhs += model.Yhs; 
                                break;
                            case "Sljj": 
                                data.Append("<td>" + model.Sljj + "</td>");
                                Sljj += model.Sljj; 
                                break;
                            case "xiaoji": 
                                data.Append("<td>" + model.xiaoji + "</td>");
                                xiaoji += model.xiaoji; 
                                break; 
                                                     
                        }
                       
                    }                   
                    data.Append("</tr>");
                }
                data.Append("<tr>");
                data.Append("<td>合计：</td>");
                data.Append("<td>" + StreetOfficeHousNumber + "</td>");
                data.Append("<td>" + StreetOfficeShopNumber + "</td>");
                data.Append("<td>" + ANumber + "</td>");
                data.Append("<td>" + PNumber + "</td>");
                data.Append("<td>" + Zzs + "</td>");
                data.Append("<td>" + Fcs + "</td>");
                data.Append("<td>" + Grsds + "</td>");
                data.Append("<td>" + Cjs + "</td>");
                data.Append("<td>" + Jyffj + "</td>");
                data.Append("<td>" + Dfjyfj + "</td>");
                data.Append("<td>" + Yhs + "</td>");
                data.Append("<td>" + Sljj + "</td>");
                data.Append("<td>" + xiaoji + "</td>");
                data.Append("</tr>");
                data.Append("</table>");
            }
            #endregion

            string sw = ExcelHelper.ExcelTemplate(data.ToString());

            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss");
            // 写入到数据流
            return File(System.Text.Encoding.UTF8.GetBytes(sw.ToString()), "application/octet-stream", "综合治税街道办统计报表_" + fileName + ".xls");

        }
        
        /// <summary>
        /// 导出社区统计报表
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        public FileResult GIS_CommunityStatisticalTableMeg(FormCollection form)
        {
            string GIS_CommunityStatisticalTableMeg = form["GIS_CommunityStatisticalTableMeg"];
            string where = form["where"];
            int soID = StreetOfficeId;

            string[] str_arr = GIS_CommunityStatisticalTableMeg.Split(',');
            NoSortHashTable nameList = new NoSortHashTable();

            for (int i = 0; i < str_arr.Length; i++)
            {
                if (str_arr[i] != "")
                {
                    string[] item = str_arr[i].Split('|');
                    nameList.Add(item[0], item[1]);
                }
            }
            #region 拼接table

            System.Text.StringBuilder data = new System.Text.StringBuilder();
            if (nameList != null)
            {
                data.Append("<table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\">");
                //写出列名
                data.Append("<tr style=\"font-weight: bold; white-space: nowrap;\">");

                foreach (string s in nameList.Keys)
                {
                    data.Append("<td>" + nameList[s] + "</td>");
                }
                data.Append("</tr>");

                string stateDate = "";
                string endDate = "";
                if (!string.IsNullOrEmpty(where))
                {
                    string[] getTime = where.Split(',');
                    stateDate = getTime[0];
                    endDate = getTime[1];

                }


                List<V_GIS_StatisticalTable> list = GIS_StatisticalTable_Bll<V_GIS_StatisticalTable>.GetCommunityInfo(soID,stateDate, endDate);
                //写出数据
                int CommunityHousNumber = 0, CommunityShopNumber = 0, ANumber = 0, PNumber = 0;
                double Zzs = 0, Fcs = 0, Grsds = 0, Cjs = 0, Jyffj = 0, Dfjyfj = 0, Yhs = 0, Sljj = 0, xiaoji = 0;
                foreach (V_GIS_StatisticalTable model in list)
                {
                    data.Append("<tr>");
                    foreach (string sm in nameList.Keys)
                    {
                        switch (sm)
                        {
                            case "Community": 
                                data.Append("<td>" + model.Community + "</td>"); 
                                break;
                            case "CommunityHousNumber":
                                data.Append("<td>" + model.CommunityHousNumber + "</td>");
                                CommunityHousNumber += model.CommunityHousNumber;
                                break;
                            case "CommunityShopNumber":
                                data.Append("<td>" + model.CommunityShopNumber + "</td>");
                                CommunityShopNumber += model.CommunityShopNumber;
                                break;
                            case "CommunityANumber":
                                data.Append("<td>" + model.ANumber + "</td>");
                                ANumber += model.ANumber;
                                break;
                            case "CommunityPNumber":
                                data.Append("<td>" + model.PNumber + "</td>");
                                PNumber += model.PNumber;
                                break;
                            case "CommunityZzs":
                                data.Append("<td>" + model.Zzs + "</td>");
                                Zzs += model.Zzs;
                                break;
                            case "CommunityFcs":
                                data.Append("<td>" + model.Fcs + "</td>");
                                Fcs += model.Fcs;
                                break;
                            case "CommunityGrsds":
                                data.Append("<td>" + model.Grsds + "</td>");
                                Grsds += model.Grsds;
                                break;
                            case "CommunityCjs":
                                data.Append("<td>" + model.Cjs + "</td>");
                                Cjs += model.Cjs;
                                break;
                            case "CommunityJyffj":
                                data.Append("<td>" + model.Jyffj + "</td>");
                                Jyffj += model.Jyffj;
                                break;
                            case "CommunityDfjyfj":
                                data.Append("<td>" + model.Dfjyfj + "</td>");
                                Dfjyfj += model.Dfjyfj;
                                break;
                            case "CommunityYhs":
                                data.Append("<td>" + model.Yhs + "</td>");
                                Yhs += model.Yhs;
                                break;
                            case "CommunitySljj":
                                data.Append("<td>" + model.Sljj + "</td>");
                                Sljj += model.Sljj;
                                break;
                            case "Communityxiaoji":
                                data.Append("<td>" + model.xiaoji + "</td>");
                                xiaoji += model.xiaoji;
                                break;

                        }

                    }
                    data.Append("</tr>");
                }
                data.Append("<tr>");
                data.Append("<td>合计：</td>");
                data.Append("<td>" + CommunityHousNumber + "</td>");
                data.Append("<td>" + CommunityShopNumber + "</td>");
                data.Append("<td>" + ANumber + "</td>");
                data.Append("<td>" + PNumber + "</td>");
                data.Append("<td>" + Zzs + "</td>");
                data.Append("<td>" + Fcs + "</td>");
                data.Append("<td>" + Grsds + "</td>");
                data.Append("<td>" + Cjs + "</td>");
                data.Append("<td>" + Jyffj + "</td>");
                data.Append("<td>" + Dfjyfj + "</td>");
                data.Append("<td>" + Yhs + "</td>");
                data.Append("<td>" + Sljj + "</td>");
                data.Append("<td>" + xiaoji + "</td>");
                data.Append("</tr>");
                data.Append("</table>");
            }
            #endregion

            string sw = ExcelHelper.ExcelTemplate(data.ToString());

            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss");
            // 写入到数据流
            return File(System.Text.Encoding.UTF8.GetBytes(sw.ToString()), "application/octet-stream", "综合治税社区统计报表_" + fileName + ".xls");

        }
        #endregion

        #region - 年度任务完成表控制器 -


        public ActionResult GIS_AnnualTask()
        {
            return View();
        }


        /// <summary>
        /// 年度任务完成列表
        /// </summary>
        /// <param name="index"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public ActionResult GetGIS_AnnualTask(string where)
        {
            List<V_GIS_AnnualTask> lst_AnnualTask = GIS_AnnualTask_Bll<V_GIS_AnnualTask>.GetGIS_AnnualTask(where);
            //TempData["QueryConditions"] = where;//给导出数据传查询条件
            return Content(lst_AnnualTask.ToJson());
        }




        /// <summary>
        /// 年度任务完成列表
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        
        
        public FileResult GIS_AnnualTaskMeg(FormCollection form)
        {
            string GIS_AnnualTaskMeg = form["GIS_AnnualTaskMeg"];
            string where = form["where"];
            string[] str_arr = GIS_AnnualTaskMeg.Split(',');
            NoSortHashTable nameList = new NoSortHashTable();

            for (int i = 0; i < str_arr.Length; i++)
            {
                if (str_arr[i] != "")
                {
                    string[] item = str_arr[i].Split('|');
                    nameList.Add(item[0], item[1]);
                }
            }

            #region 拼接table

            System.Text.StringBuilder data = new System.Text.StringBuilder();
            if (nameList != null)
            {
                data.Append("<table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\">");
                //写出列名
                data.Append("<tr style=\"font-weight: bold; white-space: nowrap;\">");

                foreach (string s in nameList.Keys)
                {
                    data.Append("<td>" + nameList[s] + "</td>");
                }
                data.Append("</tr>");

                List<V_GIS_AnnualTask> list = GIS_AnnualTask_Bll<V_GIS_AnnualTask>.GetGIS_AnnualTask(where);
                //写出数据
                foreach (V_GIS_AnnualTask model in list)
                {
                    data.Append("<tr>");
                    foreach (string sm in nameList.Keys)
                    {
                        switch (sm)
                        {
                            case "StreetOffice": data.Append("<td>" + model.StreetOffice + "</td>"); ; break;
                            case "StreetOfficeHousNumber": data.Append("<td>" + model.StreetOfficeHousNumber + "</td>"); ; break;
                            case "HousNumAT": data.Append("<td>1000</td>"); ; break;
                            case "HNBfb": data.Append("<td>" + (model.StreetOfficeHousNumber * 0.1)+"%" + "</td>"); ; break;
                            case "StreetOfficeShopNumber": data.Append("<td>" + model.StreetOfficeShopNumber + "</td>"); ; break;
                            case "ShopNumAT": data.Append("<td>1000</td>"); ; break;
                            case "SNBfb": data.Append("<td>" + (model.StreetOfficeShopNumber*0.1)+"%" + "</td>"); ; break;
                            case "ANumber": data.Append("<td>" + model.ANumber + "</td>"); ; break;
                            case "ANumberAT": data.Append("<td>1000</td>"); ; break;
                            case "ANumberBfb": data.Append("<td>" + (model.ANumber*0.1)+ "%"+ "</td>"); ; break;
                            case "xiaoji": data.Append("<td>" + model.xiaoji*0.01 + "</td>"); ; break;
                            case "AtRevenuePlan": data.Append("<td>" + model.AtRevenuePlan*0.01 + "</td>"); ; break;                              
                            case "AtRevenuePlanBfb":
                                if (model.AtRevenuePlan != 0)
                                {
                                    data.Append("<td>" + Math.Round(((model.xiaoji / model.AtRevenuePlan) * 100), 2) + "%" + "</td>"); ;
                                }
                                else
                                {
                                    data.Append("<td> / </td>"); ;
                                }
                                break;
                                                                        
                        }
                    }
                    data.Append("</tr>");
                }

                data.Append("</table>");
            }
            #endregion

            string sw = ExcelHelper.ExcelTemplate(data.ToString());

            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss");
            // 写入到数据流
            return File(System.Text.Encoding.UTF8.GetBytes(sw.ToString()), "application/octet-stream", "年度任务完成列表_" + fileName + ".xls");

        }
        #endregion

        #region - 年度税收统计表控制器 -  
  
    
        public ActionResult YearTaxStat()
        {
            return View();
        }


        /// <summary>
        /// 年度税收统计表
        /// </summary>
        /// <param name="input">搜索条件</param>
        /// <returns></returns>
        public ActionResult GetYearTaxStat(string where)
        {
            List<V_YearTaxStat> lst_YearTaxStat = YearTaxStat_Bll<V_YearTaxStat>.GetYearTaxStatList(where);
            TempData["QueryConditions"] = where;//给导出数据传查询条件
            return Content(lst_YearTaxStat.ToJson());
        }

        /// <summary>
        /// 导出综合治税社区统计报表
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        public FileResult YearTaxStatMeg(FormCollection form)
        {
            string YearTaxStatMeg = form["YearTaxStatMeg"];
            string where = form["where"];
            string[] str_arr = YearTaxStatMeg.Split(',');
            NoSortHashTable nameList = new NoSortHashTable();

            for (int i = 0; i < str_arr.Length; i++)
            {
                if (str_arr[i] != "")
                {
                    string[] item = str_arr[i].Split('|');
                    nameList.Add(item[0], item[1]);
                }
            }

            #region 拼接table

            System.Text.StringBuilder data = new System.Text.StringBuilder();
            if (nameList != null)
            {
                data.Append("<table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\">");
                //写出列名
                data.Append("<tr style=\"font-weight: bold; white-space: nowrap;\">");

                foreach (string s in nameList.Keys)
                {
                    data.Append("<td>" + nameList[s] + "</td>");
                }
                data.Append("</tr>");

                List<V_YearTaxStat> list = YearTaxStat_Bll<V_YearTaxStat>.GetYearTaxStatList(where);
                //写出数据
                double January = 0, February=0, Marcy=0, April=0, May=0, June=0, July=0, August=0, September=0, October=0, November=0, December=0,xiaoji1=0;
                foreach (V_YearTaxStat model in list)
                {
                    data.Append("<tr>");
                    foreach (string sm in nameList.Keys)
                    {
                        var xiaoji = (model.January + model.February + model.Marcy + model.April + model.May + model.June + model.July + model.August + model.September + model.October + model.November + model.December)*0.01;
                        switch (sm)
                        {
                            case "Dt_Name": data.Append("<td>" + model.Dt_Name + "</td>"); ; break;
                            case "January": 
                                data.Append("<td>" + model.January*0.01 + "</td>");
                                January +=model.January*0.01; 
                                break;
                            case "February": 
                                data.Append("<td>" + model.February*0.01 + "</td>");
                                February += model.February * 0.01; 
                                break;
                            case "Marcy": 
                                data.Append("<td>" + model.Marcy*0.01 + "</td>");
                                Marcy += model.February * 0.01; 
                                break;
                            case "April": 
                                data.Append("<td>" + model.April*0.01 + "</td>");
                                April += model.April * 0.01; 
                                break;
                            case "May": 
                                data.Append("<td>" + model.May*0.01 + "</td>");
                                May += model.May * 0.01; 
                                break;
                            case "June": 
                                data.Append("<td>" + model.June*0.01 + "</td>");
                                June += model.June * 0.01; 
                                break;
                            case "July": 
                                data.Append("<td>" + model.July*0.01 + "</td>");
                                July += model.July * 0.01; 
                                break;
                            case "August": 
                                data.Append("<td>" + model.August*0.01 + "</td>");
                                August += model.August * 0.01; 
                                break;
                            case "September": 
                                data.Append("<td>" + model.September*0.01 + "</td>");
                                September += model.September * 0.01; 
                                break;
                            case "October": 
                                data.Append("<td>" + model.October*0.01 + "</td>");
                                October += model.October * 0.01; 
                                break;
                            case "November": 
                                data.Append("<td>" + model.November*0.01 + "</td>");
                                November += model.November * 0.01; 
                                break;
                            case "December": 
                                data.Append("<td>" + model.December*0.01 + "</td>");
                                December += model.December * 0.01; 
                                break;
                            case "xiaoji": 
                                data.Append("<td>" + xiaoji + "</td>");
                                xiaoji1 += xiaoji; 
                                break;                           
                        }
                    }
                    data.Append("</tr>");
                }
                data.Append("<td>合计：</td>");
                data.Append("<td>" + January + "</td>");
                data.Append("<td>" + February + "</td>");
                data.Append("<td>" + Marcy + "</td>");
                data.Append("<td>" + April + "</td>");
                data.Append("<td>" + May + "</td>");
                data.Append("<td>" + June + "</td>");
                data.Append("<td>" + July + "</td>");
                data.Append("<td>" + August + "</td>");
                data.Append("<td>" + September + "</td>");
                data.Append("<td>" + October + "</td>");
                data.Append("<td>" + November + "</td>");
                data.Append("<td>" + December + "</td>");
                data.Append("<td>" + xiaoji1 + "</td>");
                data.Append("</tr>");
                data.Append("</table>");
            }
            #endregion

            string sw = ExcelHelper.ExcelTemplate(data.ToString());

            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss");
            // 写入到数据流
            return File(System.Text.Encoding.UTF8.GetBytes(sw.ToString()), "application/octet-stream", "年度税收统计_" + fileName + ".xls");

        }

        #endregion
    }
}
